<?php
include_once 'BaseS.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/NewsM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/BloodMessageM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/MemberM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/MemberTokenM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Util.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Regex.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/ImgUploads.php';

class AppNewsS extends BaseS {
	
	public function __construct() {
		parent::__construct ();
		parent::setTarget ( $this );
	}
	
	public function queryBoth(){
		
		$result = array("status" => 0, "msg" => "", "newsCount" => 0, "BMCount" => 0, "news" => array(), "BM" => array());
		
		$phoneType 			= $_POST['phoneType'];
		$phoneVersion 		= $_POST['phoneVersion'];
		$newsStart 			= $_POST['newsStart'] ;
		$newsEnd 			= $_POST['newsEnd'];
		$bloodMessageStart 	= $_POST['BMStart'];
		$bloodMessageEnd 	= $_POST['BMEnd'];
		
		$newsDao = new NewsM();
		$bloodMessageDao = new BloodMessageM();
		
		if(!Regex::isNumber($phoneType, 1))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
		if(!Regex::isFloat($phoneVersion, 30))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
		
		if($phoneType == 1 && $phoneVersion <= ANDROID_1 ){
		
			if($newsStart <> 0 ){
				if(!Regex::isNumber($newsStart, 11))			{$result["status"] = -3; Util::toEchoJson($result); exit;}
			}
			
			if($bloodMessageStart <> 0 ){
				if(!Regex::isNumber($bloodMessageStart, 11))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
			}
			
			if(!Regex::isNumber($newsEnd, 11))				{$result["status"] = -3; Util::toEchoJson($result); exit;}
			if(!Regex::isNumber($bloodMessageEnd, 11))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
			
			$news = $newsDao->query($newsStart, $newsEnd);
			$bloodMessage = $bloodMessageDao->query($bloodMessageStart, $bloodMessageEnd);
			
			if($news['count'] > 0 && $bloodMessage['count'] > 0){
				$result['status'] = 1;
				$result['newsCount'] = $news['count'];
				$result['BMCount'] = $bloodMessage['count'];
				$result['news'] = $news['result'];
				$result['BM'] = $bloodMessage['result'];
			}else if($news['count'] > 0 && $bloodMessage['count'] <= 0){
				$result['status'] = 2;
				$result['newsCount'] = $news['count'];
				$result['news'] = $news['result'];
			}else if($news['count'] <= 0 && $bloodMessage['count'] > 0){
				$result['status'] = 3;
				$result['BMCount'] = $bloodMessage['count'];
				$result['BM'] = $bloodMessage['result'];
			}else if($news['count'] <= 0 && $bloodMessage['count'] <= 0){
				$result['status'] = 4;
			}else{
				$result['status'] = -3;
			}
			
			Util::toEchoJson($result);
			exit;
			
		}else{
			
			Util::toEchoJson($result);
			exit;
			
		}
	}
	
	public function queryNews(){
	
		$result = array("status" => 0, "msg" => "", "newsCount" => 0, "news" => array());
	
		$phoneType 			= $_POST['phoneType'];
		$phoneVersion 		= $_POST['phoneVersion'];
		$newsStart 			= $_POST['newsStart'] ;
		$newsEnd 			= $_POST['newsEnd'];
	
		$newsDao = new NewsM();
	
		if(!Regex::isNumber($phoneType, 1))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
		if(!Regex::isFloat($phoneVersion, 30))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
	
		if($phoneType == 1 && $phoneVersion <= ANDROID_1 ){
	
			if($newsStart <> 0 ){
				if(!Regex::isNumber($newsStart, 11))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
			}
			
			if(!Regex::isNumber($newsEnd, 11))				{$result["status"] = -3; Util::toEchoJson($result); exit;}
		
			$news = $newsDao->query($newsStart, $newsEnd);
				
			if($news['count'] > 0 ){
				$result['status'] = 2;
				$result['newsCount'] = $news['count'];
				$result['news'] = $news['result'];
			}else if($news['count'] <= 0){
				$result['status'] = 4;
			}else{
				$result['status'] = -3;
			}
				
			Util::toEchoJson($result);
			exit;
				
		}else{
			Util::toEchoJson($result);
			exit;
		}
	}
	
	public function queryBM(){
		
		$result = array("status" => 0, "msg" => "", "BMCount" => 0, "BM" => array());
		
		$phoneType 			= $_POST['phoneType'];
		$phoneVersion 		= $_POST['phoneVersion'];
		$bloodMessageStart 	= $_POST['BMStart'];
		$bloodMessageEnd 	= $_POST['BMEnd'];
		
		$bloodMessageDao = new BloodMessageM();
		
		if(!Regex::isNumber($phoneType, 1))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
		if(!Regex::isFloat($phoneVersion, 30))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
		
		if($phoneType == 1 && $phoneVersion <= ANDROID_1 ){
		
			if($bloodMessageStart <> 0 ){
				if(!Regex::isNumber($bloodMessageStart, 11))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
			}
			
			if(!Regex::isNumber($bloodMessageEnd, 11))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
			
			$bloodMessage = $bloodMessageDao->query($bloodMessageStart, $bloodMessageEnd);
			
			if($bloodMessage['count'] > 0){
				$result['status'] = 3;
				$result['BMCount'] = $bloodMessage['count'];
				$result['BM'] = $bloodMessage['result'];
			}else if($bloodMessage['count'] <= 0){
				$result['status'] = 4;
			}else{
				$result['status'] = -3;
			}
			
			Util::toEchoJson($result);
			exit;
			
		}else{
			Util::toEchoJson($result);
			exit;
		}
	}
	
	public function getBM(){
	
		$result = array("status" => 0, "msg" => "", "BMId" => 0, "species" => "", "blood" => "", "hp_name" => "", "hp_address" => "", "hp_phone" => "", "photo" => "", "content" => "");
	
		$phoneType 			= $_POST['phoneType'];
		$phoneVersion 		= $_POST['phoneVersion'];
		$id 				= $_POST['BMId'];
	
		$bloodMessageDao = new BloodMessageM();
	
		if(!Regex::isNumber($phoneType, 1))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
		if(!Regex::isFloat($phoneVersion, 30))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
	
		if($phoneType == 1 && $phoneVersion <= ANDROID_1 ){
	
			$bloodMessage = $bloodMessageDao->getById($id);
				
			if($bloodMessage){
				$result = array(
						"status" => 1, 
						"msg" => "", 
						"BMId" => $bloodMessage->id, 
						"species" => $bloodMessage->species, 
						"blood" => $bloodMessage->blood, 
						"hp_name" => $bloodMessage->hp_name, 
						"hp_address" => $bloodMessage->hp_address, 
						"hp_phone" => $bloodMessage->hp_phone, 
						"photo" => $bloodMessage->photo,
						"content" => $bloodMessage->content
						);
				
			}else{
				$result['status'] = -3;
			}
			
			Util::toEchoJson($result);
			exit;
		}else{
			Util::toEchoJson($result);
			exit;
		}
	}
	
	public function sendBloodMessage(){
		
		$result = array("status" => 0, "msg" => "", "photoName" => "");
		
		$phoneType 		= $_POST['phoneType'];
		$phoneVersion 	= $_POST['phoneVersion'];
		$memberId 		= isset($_POST['mId']) ? $_POST['mId'] : 0 ;
		$deviceKey 		= isset($_POST['token']) ? $_POST['token'] : "" ;
		$species		= isset($_POST['species']) ? $_POST['species'] : "" ;
		$blood			= isset($_POST['blood']) ? $_POST['blood'] : "" ;
		$hpName			= isset($_POST['hpName']) ? $_POST['hpName'] : "" ;
		$hpAddress		= isset($_POST['hpAddress']) ? $_POST['hpAddress'] : "" ;
		$hpPhone		= isset($_POST['hpPhone']) ? $_POST['hpPhone'] : "" ;
		$content		= isset($_POST['content']) ? $_POST['content'] : "" ;
		
		$checkToken = new MemberTokenM();
		$bloodMessageDao = new BloodMessageM();
		$dao = new MemberM();
		$img = new ImgUploads();
		
		if(!Regex::isNumber($phoneType, 1))		{$result["status"] = -3; Util::toEchoJson($result); exit;}
		if(!Regex::isFloat($phoneVersion, 30))	{$result["status"] = -3; Util::toEchoJson($result); exit;}
		
		if($phoneType == 1 && $phoneVersion <= ANDROID_1 ){
		
			if(!$checkToken->checkToken($memberId, $deviceKey)) {$result["status"] = -4; Util::toEchoJson($result); exit;}
			//if(!$dao->checkLastSendBloodMessage($memberId)) {$result["status"] = -11; Util::toEchoJson($result); exit;}
			
			$photoPath = $_SERVER ['DOCUMENT_ROOT'].BM_PHOTO_PATH;
			$photoName = $img->ImgUpload($photoPath, $memberId.Util::getNowDate("YmdHis"));
			if(!$photoName){$result["status"] = -12; Util::toEchoJson($result); exit;}
			
			$obj = array(
					"member_id" => $memberId,
					"species" => $species,
					"blood" => $blood,
					"hp_name" => $hpName,
					"hp_address" => $hpAddress,
					"hp_phone" => $hpPhone,
					"photo" => $photoName ,
					"content" => $content,
					"date_create" => Util::getNowDate()
			);
			
			$ret = $bloodMessageDao->insert($obj);
			
			if($ret){
				$dao->updateLastBM($memberId);
				$result['status'] = 1;
				$result['photoName'] = $photoName;
				Util::toEchoJson($result);
				exit;
			}else{
				Util::toEchoJson($result);
				exit;
			}
			
		}else{
			Util::toEchoJson($result);
			exit;
		}
		
	}
	
}

if (isset($_POST["action"])) {
	$service = new AppNewsS ();
	$service->__call ( $_POST["action"], null );
} 
?>